<template>
  <section>
    <el-row style="margin: -3px 0px 0px 1px">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/system/dashboard' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>系统管理</el-breadcrumb-item>
        <el-breadcrumb-item>服务管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>

    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" style="float:left;margin: 18px 27px -3px">
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <category-query @SearchByName="listCategory" ref="searchBar"/>
        </el-form-item>
      </el-form>
    </el-col>

    <el-card style="margin: 18px 2%;width: 95%">
      <el-table
        :data="menuList"
        stripe
        :default-sort="{prop: 'id', order: 'ascending'}"
        style="width: 100%"
        :max-height="tableHeight">
        <!--Form-->
        <el-table-column prop="id" label="id" sortable/>
        <el-table-column prop="name" label="名字" fit/>
        <el-table-column prop="description" label="描述" fit/>

        <el-table-column label="操作" width="140">
          <template slot-scope="scope">
            <el-button
              @click="editCategory(scope.row)"
              type="text"
              size="small">
              编辑
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="deleteCategory(scope.row)">
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-col :span="24" class="toolbar">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 20]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-card>
    <category-add_-update ref="editOrAddDialog" @reload="listCategory"/>
  </section>

</template>

<script>

import CategoryQuery from "./component/CategoryQuery";
import CategoryAdd_Update from "./component/CategoryAdd_Update";

export default {
  name: "CategoryIndex",
  components: {CategoryAdd_Update, CategoryQuery},
  data() {
    return {
      menuList: [],
      dialogFormVisible: false,
      size: 5,
      total: 0,
      page: 1,
    }
  },
  computed: {
    tableHeight() {
      return window.innerHeight - 320
    }
  },
  mounted() {
    this.listCategory();
  },
  methods: {
    listCategory() {
      this.$axios.post("/category/queryPage", {
        page: this.page,
        size: this.size,
        name: this.$refs.searchBar.name
      }).then(resp => {
        if (resp.status === 200 && resp) {
          this.menuList = resp.data.result
          this.total = resp.data.total
        }
      })
    },
    handleAdd() {
      this.$refs.editOrAddDialog.add();
    },
    editCategory(row) {
      this.$refs.editOrAddDialog.edit(row)
    },
    deleteCategory(row) {
      this.$confirm('确定删除' + row.name + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning"
      }).then(() => {
        var data = Object.assign({},row)
        this.$axios.post("/category/deleteOne",data).then(resp=>{
          if(resp.data.code===200){
            this.$message({
              message:resp.data.result,
              type:"success"
            })
          }
          this.listCategory()
        })
      })
    },
    handleSizeChange(val) {
      this.size = val
      this.listCategory()
    },
    handleCurrentChange(page) {
      this.page = page
      this.listCategory()
    },
  }
}
</script>

<style scoped>

</style>
